package com.onlineproject.action;

import com.onlineproject.model.Project;
import com.onlineproject.model.Student;
import com.onlineproject.model.StuPro;
import com.onlineproject.dao.ProjectDao;
import com.onlineproject.dao.SearchDao;
import com.onlineproject.dao.StuProDao;
import com.onlineproject.dao.StudentDao;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import com.onlineproject.dao.SearchDao;

public class SearchAction {

	private SearchDao searchDao;
	
	private String gardenName;
	
	
	public String getGardenName() {
		return gardenName;
	}


	public void setGardenName(String gardenName) {
		this.gardenName = gardenName;
	}


	public SearchDao getSearchDao() {
		return searchDao;
	}


	public void setSearchDao(SearchDao searchDao) {
		this.searchDao = searchDao;
	}


	public SearchAction()
	{
		ClassPathResource resource = new ClassPathResource("applicationContext.xml");
    	XmlBeanFactory factory = new XmlBeanFactory(resource);
    	searchDao = (SearchDao)factory.getBean("searchDao");	
    	
	}
	
	
	public String GetProjectByGarden() throws UnsupportedEncodingException
	{
		//HttpServletRequest  request=ServletActionContext.getRequest();
		String gName= ServletActionContext.getRequest().getParameter("gName");
		
		gardenName=java.net.URLDecoder.decode(gName, "GB2312");
			
		gardenName=new String(gardenName.getBytes("ISO-8859-1"));
		try{
			List datas = new ArrayList();
			List l;
			datas=searchDao.GetProjectByGarden(gardenName);
			
			if(!datas.isEmpty()){
		    
				ServletActionContext.getRequest().setAttribute("searchProjectByGardenList", datas);
		     
		     return "searchProjectByGardenList";
			
			}
			
			else
				return "failure";
			
		}catch(Exception e){
			e.printStackTrace();
			return "failure";
		}	

	}
	
	public String GetStudentByGarden() throws UnsupportedEncodingException
	{
		//HttpServletRequest  request=ServletActionContext.getRequest();
		String gName= ServletActionContext.getRequest().getParameter("gName");
		
		gardenName=java.net.URLDecoder.decode(gName, "GB2312");
			
		gardenName=new String(gardenName.getBytes("ISO-8859-1"));
		try{
			List datas = new ArrayList();
			List l;
			datas=searchDao.GetStudentByGarden(gardenName);
			
			if(!datas.isEmpty()){
		    
				ServletActionContext.getRequest().setAttribute("searchStudentByGardenList", datas);
		     
		     return "searchStudentByGardenList";
			
			}
			
			else
				return "failure";
			
		}catch(Exception e){
			e.printStackTrace();
			return "failure";
		}	

	}
	
	
}
